const {http} = require("../../models/http.js")
const {timepro,numpro} = require("../../utils/music.js")
Page({
  data: {
    playlist:[],
    obj1:[],
    tracks:[],
    playStates:{},
    prevID:"",
  },

  onLoad: function (options) {
    let {id} = options;
    let url = `playlist/detail?id=${id}`;
    http({
      url,
      success:res=>{
         let playlist = res.data.playlist;
          playlist.playCount = numpro(playlist.playCount)
          playlist.subscribedCount = numpro(playlist.subscribedCount)
          playlist.commentCount = numpro(playlist.commentCount)
          playlist.shareCount = numpro(playlist.shareCount)

          if(playlist.name.length>8){
            playlist.name = playlist.name.slice(0,8)+"..."
          }
          var tracks = res.data.playlist.tracks;
          tracks.forEach(item=>{
            // console.log(item.ar);
            var ar = item.ar;
            // console.log(ar);

            item.ar = {ar};
            if(item.ar.ar[0].name.length>6){
              item.ar.ar[0].name = item.ar.ar[0].name.slice(0,5)+"...";
            }
            if(item.al.name.length>5){
              item.al.name = item.al.name.slice(0,5)+"...";
            }
            item.dt = timepro(item.dt)
        
        this.setData({
          playlist,
          tracks
        })
    })
  }
})
},

    handleClick(event){
    let id = event.currentTarget.dataset.id;
    let playStates = this.data.playStates;

    if(id !=this.data.prevID){
      playStates[id] = true;
      for(let key in playStates){
        if(key !=id){
          playStates[key] = false;
        }
      }
      this.setData({
        playStates,
        prevID:id
      })
    }else{
      playStates[id] = !playStates[id];
      this.setData({
        playStates
      })
    }
  },
})